if (typeof exports == 'undefined')
    exports = {};
//window.addEventListener('load', function () {
//    $.ajax({
//        type: 'GET',
//        dataType: 'json',
//        url: "../manifest.json",
//        success: function (json) {
//            exports.extension = json;
//            localStorage["version"] = exports.extension.version;
//        }
//    });

//    exports.storage = new Storage();
//    exports.work = new Work(exports.storage);
//    exports.work.run();

//    exports.site = this.Site;
//    alert(1);
//    chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
//        if (changeInfo.status == 'loading') {
//            var confKey = Site.url2key(null, tab.url);
//            if (confKey) {
//                if (exports.work.checkUrl(confKey, tab.url))
//                    Site.getPageInfo({
//                        tab: tab,
//                        success: function (_info) {
//                            //更新本url相关的数据信息
//                            Site.handle.updateSiteData(tab.url, _info);
//                        }
//                    });
//                chrome.pageAction.show(tab.id);
//            }
//            //设置 
//        }

//    });
//}, false);
$(function () {

    (function () {
        $.ajax({
            type: 'GET',
            dataType: 'json',
            url: "../manifest.json",
            success: function (json) {
                exports.extension = json;
                localStorage["version"] = exports.extension.version;
            }
        });

        exports.storage = new Storage();
        exports.work = new Work(exports.storage);
        exports.work.run();

        exports.site = this.Site;
        chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
            if (changeInfo.status == 'loading') {
                var confKey = Site.url2key(null, tab.url);
                if (confKey) {
                    if (exports.work.checkUrl(confKey, tab.url)) {
                        Site.getPageInfo({
                            tab: tab,
                            success: function (_info) {
                                //更新本url相关的数据信息
                                Site.handle.updateSiteData(tab.url, _info);
                            }
                        });
                        chrome.tabs.getSelected(null, function (tab) {
                            exports.tabid = tabId
                        });
                        console.log(tabId);


                    }
                    chrome.pageAction.show(tabId);
                }
                //设置 
            }

        });
    })();
    (function (a, d, c) {
        var b = d;
        var work = exports.work;
        if (!b) {
            var a = {}
        }
        if (!b.service) {
            b.service = {};
        }
        b.service = {
            add_price: function (f, e, d) {

            },
            add_url: function (f, e, d) {
                exports.work.addUrl(f.confKey, f.url);
                d();
            },
            del_url: function (f, e, d) {
                exports.work.delUrl(f.confKey, f.url);
                d();
            },
            get_url2key: function (f, e, d) {
                var url = f.url;
                var key = exports.site.url2key(null, url);

                d(key);
            },
            get_pageInfo: function (f, e, d) {
                console.log(1111);
                var url = f.url;
                var rank = f.rank;
                var confKey = f.confKey;
                console.log(url);
                exports.site.getPageInfo({
                    url: url,
                    success: function (_info) {
                        //更新本url相关的数据信息
                        _info.rank = rank;
                        console.log(555555);
                        exports.site.handle.updateSiteData(url, _info);
                    }
                }, confKey);
                console.log(11111);
                d();
            },
            get_data: function (f, e, d) {
                d(exports.work.sites);
            },
            get_template: function (f, e, d) {
                b.service.helper.getTemplate(f.page, d)
            },
            get_url: function (f, e, d) {
                b.service.helper.getUrl(f.url, d);
            },
            get_bar: function (f, e, d) {
                var url = chrome.extension.getURL('img/arrow.gif');
                var draftUrl = chrome.extension.getURL('assets/followProduct.htm');
                $.ajax({
                    url: draftUrl,
                    dataType: 'html',
                    success: function (data) {
                        var result = { url: url,
                            tmpl: data
                        };
                        d(result);
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        console.log(jqXHR);
                    }
                });
            },
            open_tab: function (f, e, d) {
                b.service.helper.openTab(f, e, d);
            },
            show_popup: function (f, e, d) {
                //                chrome.tabs.getCurrent(function (tab) {
                //                    alert(tab.id);
                //                    chrome.pageAction.show(tab.id);

                //                });

                console.log('tabid' + exports.tabid);
                chrome.pageAction.show(exports.tabid);
            },
            hide_popup: function (f, e, d) {
                /////b.NotificationsManager.hide();
            }
        };
        b.service.helper = {
            requestHandler: function (h, g, d) {
                var f = h.topic;
                //b.console.debug("request for " + f);
                try {
                    if (f.charAt(0) != "_" && typeof b.service[h.topic] == "function") {
                        b.service[h.topic](h, g, d)
                    } else {
                        //b.console.debug("unknown request " + f)
                    }
                } catch (i) {
                    //b.console.error("REQEUST ERROR:" + i.message + (i.stack ? "\n" + i.stack : ""))
                }
            },
            getTemplate: function (i, j) {
                var draftUrl = chrome.extension.getURL(i);
                $.ajax({
                    url: draftUrl,
                    dataType: 'html',
                    success: function (data) {
                        j(data);
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        console.log(jqXHR);
                    }
                });
            },
            getUrl: function (i, j) {
                var url = chrome.extension.getURL(i);
                j(url);
            },
            openTab: function (i, j) {
                var url = chrome.extension.getURL(i.url);
                chrome.tabs.create({ url: url, selected: true });
            }
        };

        //桌面信息提醒
        b.NotificationsManager = {
            //            tp: '<script> uniqueKey = "{{user.uniqueKey}}"; Timeout = {{timeout}};</script>' +
            //        '<div class="item">' +
            //            '<div class="usericon"><img src="{{user.profile_image_url}}" class="face"/><img src="images/blogs/{{user.blogType}}_16.png" class="blogType"/></div>' +
            //            '<div class="info"><span class="username">{{user.screen_name}}</span><br/>' +
            //                '<span class="unreads">' +
            //                    '<span id="unr_friends_timeline"><span>{{unreads.friends_timeline}}</span>' + _u.i18n("abb_friends_timeline") + '</span> &nbsp;&nbsp; <span id="unr_mentions"><span>{{unreads.mentions}}</span>@</span> <br/>' +
            //                    '<span id="unr_comments_timeline"><span>{{unreads.comments_timeline}}</span>' + _u.i18n("abb_comment") + '</span> &nbsp;&nbsp; <span id="unr_direct_messages"><span>{{unreads.direct_messages}}</span>' + _u.i18n("abb_direct_message") + '</span> ' +
            //                '</span>' +
            //            '</div>' +
            //        '</div>' +
            //        '<script> removeHighlight(); TIME_LINE = "{{t}}"; highlightTimeline();</script>',

            cache: {}, //存放要显示的账号
            isEnabled: function (t) {
                return getAlertMode() != 'dnd' && Settings.get().isDesktopNotifications[t];
            },
            /*
            * 先检查cache中有account有没存在，如果存在，则说明正在创建Notifications窗口
            * 如果不存在，则缓存，并创建Notifications窗口。
            * 这样是为了避免Notifications窗口还在创建中，这时chrome.extension.getViews({type:"notification"})还不能获取到该窗口，则会造成重复创建
            */
            show: function (account) {
                var ntf = webkitNotifications.createHTMLNotification('/destop_alert.html' + '#' + 'account');
                ntf.show();
            },
            //Notifications窗口创建完后，调用该方法获取信息
            getShowHtml: function (uniqueKey) {
                return 'getShowHtml';
            }
        };

        chrome.extension.onRequest.addListener(b.service.helper.requestHandler);
    })(window, exports);
});

    
    